<?php
declare(strict_types=1);

namespace app\common\model\video;

use app\common\model\BaseModel;
use think\model\relation\HasOne;

/**课程模型
 * Class Course
 * @package app\common\model
 */
class CoursePart extends BaseModel
{
    protected $pk = 'id';
    protected $name = 'video_part';

    /**
     * 关联用户头像表
     * @return HasOne
     */
    public function video(): HasOne
    {
        $module = self::getCalledModule() ?:'common';
        return $this->hasOne("app\\{$module}\\model\\UploadFile", 'file_id', 'url');
    }

    public function image(){
        $module = self::getCalledModule() ?:'common';
        return $this->hasOne("app\\{$module}\\model\\UploadFile", 'file_id', 'image')
            ->bind(['file_path','file_url','file_name']);
    }

    public function course(){
        $module = self::getCalledModule() ?:'common';
        return $this->belongsTo("app\\{$module}\\model\\video\\Course",'c_id','id');
    }

    // 添加
    public function add($params)
    {
        $params['wxapp_id'] = self::$wxapp_id;
        $params['create_time'] = time();
        return $this->insert($params) != false;
    }

    public static function detail($id){
        return self::get($id,['video','image']);
    }

    public function getCoursePart($c_id){
        return $this->where(['c_id'=>$c_id])->select();
    }
}
